﻿Public Class frStart

#Region "Declarations"
    Private previousMessageCount As Integer
    Private lGlobalStatus As objGlobalStatus

    Private Const BUBBLE_DISPLAY_TIME As Integer = 5000
#End Region

#Region "Custom Methods"

    ''' <summary>
    ''' Ruft Einstellungsdialog auf
    ''' </summary>
    ''' <remarks></remarks>
    Private Function displaySettings() As System.Windows.Forms.DialogResult
        Dim settings As New frSettings
        Return settings.ShowDialog()
    End Function

    ''' <summary>
    ''' Audiohinweis neue Nachricht
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub playSoundNotification()
        If My.Settings.SoundNotification Then
            My.Computer.Audio.Play(My.Resources.NewMessage, AudioPlayMode.Background)
        End If
    End Sub

    ''' <summary>
    ''' Bubble mit Anzahl neuer Nachrichten einblenden
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub setTrayText(ByVal newMessage As Boolean)
        Debug.Print("setTrayText()" + notifierFunctions.getStatusSummary(lGlobalStatus))
        trayIcon.Text = notifierFunctions.getStatusSummary(lGlobalStatus)

        trayIcon.BalloonTipTitle = notifierFunctions.getShortNewMessageText(lGlobalStatus.countUnreadMessages)
        trayIcon.BalloonTipText = notifierFunctions.getLongNewMessageText(lGlobalStatus.countUnreadMessages)

        If newMessage Then
            trayIcon.ShowBalloonTip(BUBBLE_DISPLAY_TIME)
        End If
    End Sub

    ''' <summary>
    ''' Einfachklick Handler: Bubble anzeigen
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub handleLeftClick()
        toggleBlinker(False)
        setTrayText(True)
    End Sub

    ''' <summary>
    ''' Doppelklick Handler: GAys.de öffnen
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub handleLeftDoubleClick()
        toggleBlinker(False)

        Process.Start("http://www.gays.de")
    End Sub

    ''' <summary>
    ''' Startet den Timer zum Nachrichtencheck
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub startChecker()
        With tmComm
            .Interval = My.Settings.RefreshPeriod * 1000
            .Start()
        End With

        toggleBlinker(False) ' Icon initialisieren
        refreshGlobalStatus()

    End Sub

    ''' <summary>
    ''' Prüft, ob Zugangsdaten gültig sind
    ''' </summary>
    ''' <returns>True, wenn Zugangsdaten gültig</returns>
    ''' <remarks></remarks>
    Private Function checkCredentials() As Boolean
        Dim g As New gaysComm(My.Settings.GaysUsername, (My.Settings.GaysPassword))
        Return g.checkCredentials
    End Function

#End Region

#Region "Blinker"

    ''' <summary>
    ''' Trayicon festlegen
    ''' </summary>
    ''' <param name="Icon">Icon vom Typ System.Drawing.Icon</param>
    ''' <remarks></remarks>
    Private Sub setTrayIcon(ByVal Icon As System.Drawing.Icon)
        trayIcon.Icon = Icon
    End Sub

    ''' <summary>
    ''' Tray-Blinker ein/ausschalten
    ''' </summary>
    ''' <param name="Enabled">True zum Einschalten</param>
    ''' <remarks></remarks>
    Private Sub toggleBlinker(ByVal Enabled As Boolean)
        setTrayIcon(My.Resources.GaysSw)
        tmBlink.Enabled = Enabled
    End Sub

    ''' <summary>
    ''' Eventhandler für Blinker-Tick
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub tmBlink_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmBlink.Tick
        Static Colour As Boolean

        If Colour Then
            setTrayIcon(My.Resources.GaysSw)
        Else
            setTrayIcon(My.Resources.GaysCol)
        End If

        Colour = Not Colour
    End Sub

#End Region

#Region "Gays Comm"


    ''' <summary>
    ''' Prüft ob neue Nachricht eingegangen ist
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub refreshGlobalStatus(Optional ByVal setOnlineStatus As Integer = 0)
        Try

            Dim g As New gaysComm(My.Settings.GaysUsername, My.Settings.GaysPassword)

            lGlobalStatus = g.getGlobalStatus
            previousMessageCount = lGlobalStatus.countUnreadMessages

            ' Neue Nachricht?
            Dim popupBubble As Boolean = (lGlobalStatus.countUnreadMessages > previousMessageCount And lGlobalStatus.countUnreadMessages > 0)

            setTrayText(popupBubble)

            If popupBubble Then
                toggleBlinker(True)
                playSoundNotification()
            Else
                toggleBlinker(False)
            End If

        Catch ex As Exception
            'TODO: Programm soll nicht abstürzen, aber wir müssen hier loggen
        End Try

    End Sub
#End Region

#Region "Form Events"

    ''' <summary>
    ''' Ruft Methode zum Nachrichtencheck auf
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub tmComm_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmComm.Tick
        refreshGlobalStatus()
    End Sub

    ''' <summary>
    ''' Programmstart
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim g As New gaysComm(My.Settings.GaysUsername, My.Settings.GaysPassword)

        Me.Size = New System.Drawing.Size(0, 0)

        ' Erster Programmstart?
        If My.Settings.FirstBoot Then
            notifierFunctions.autostartEnabled = True
            My.Settings.FirstBoot = False
        End If

        ' Zugangsdaten gültig?, wenn Abbrechen geklickt, beenden
        If Not g.checkCredentials Then
            If displaySettings() = Windows.Forms.DialogResult.Cancel Then End
        End If

        ' Timer zum Check starten
        startChecker()

    End Sub

#End Region

#Region "Tray Events"

    Private Sub trayIcon_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles trayIcon.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Left Then
            handleLeftClick()
        End If
    End Sub

    Private Sub trayIcon_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles trayIcon.MouseDoubleClick
        If e.Button = Windows.Forms.MouseButtons.Left Then
            handleLeftDoubleClick()
        End If
    End Sub

    Private Sub trayIcon_BalloonTipClicked(ByVal sender As Object, ByVal e As System.EventArgs) Handles trayIcon.BalloonTipClicked
        handleLeftDoubleClick()
    End Sub

    Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
        displaySettings()
    End Sub

    Private Sub ToolStripMenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem3.Click
        trayIcon.Dispose()
        End
    End Sub

    Private Sub ÜberGAYSDENotifierToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ÜberGAYSDENotifierToolStripMenuItem.Click
        Dim about As New frAbout
        frAbout.ShowDialog()
    End Sub

    Private Sub JetztNeuPrüfenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles JetztNeuPrüfenToolStripMenuItem.Click
        refreshGlobalStatus()
    End Sub

#End Region

#Region "Menu Online Status Events"

    Private Sub setOnlineStatus(ByVal status As Integer, ByVal menuItem As System.Windows.Forms.ToolStripMenuItem)
        Dim g As New gaysComm(My.Settings.GaysUsername, My.Settings.GaysPassword)
        g.setOnlineStatus(status)
        IchMöchteChattenToolStripMenuItem.Checked = False
        LustAufEinDateToolStripMenuItem.Checked = False
        LustAufSexToolStripMenuItem.Checked = False
        FreundeFindenToolStripMenuItem.Checked = False
        PartyAusgToolStripMenuItem.Checked = False
        EinfachFlirtenToolStripMenuItem.Checked = False
        IchMöchteVideochattenToolStripMenuItem.Checked = False
        ZurzeitNichtAmPCToolStripMenuItem.Checked = False
        menuItem.Checked = True

    End Sub

    Private Sub IchMöchteChattenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IchMöchteChattenToolStripMenuItem.Click
        setOnlineStatus(1, CType(sender, System.Windows.Forms.ToolStripMenuItem))
    End Sub

    Private Sub LustAufEinDateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LustAufEinDateToolStripMenuItem.Click
        setOnlineStatus(2, CType(sender, System.Windows.Forms.ToolStripMenuItem))
    End Sub

    Private Sub LustAufSexToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LustAufSexToolStripMenuItem.Click
        setOnlineStatus(3, CType(sender, System.Windows.Forms.ToolStripMenuItem))
    End Sub

    Private Sub FreundeFindenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FreundeFindenToolStripMenuItem.Click
        setOnlineStatus(4, CType(sender, System.Windows.Forms.ToolStripMenuItem))
    End Sub

    Private Sub PartyAusgToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PartyAusgToolStripMenuItem.Click
        setOnlineStatus(5, CType(sender, System.Windows.Forms.ToolStripMenuItem))
    End Sub

    Private Sub EinfachFlirtenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EinfachFlirtenToolStripMenuItem.Click
        setOnlineStatus(6, CType(sender, System.Windows.Forms.ToolStripMenuItem))
    End Sub

    Private Sub IchMöchteVideochattenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IchMöchteVideochattenToolStripMenuItem.Click
        setOnlineStatus(7, CType(sender, System.Windows.Forms.ToolStripMenuItem))
    End Sub

    Private Sub ZurzeitNichtAmPCToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ZurzeitNichtAmPCToolStripMenuItem.Click
        setOnlineStatus(99, CType(sender, System.Windows.Forms.ToolStripMenuItem))
    End Sub

#End Region
End Class
